package com.yunxi.service.dubboService.impl.dealer;

import com.aiways.center.channel.api.IChannelApi;
import com.alibaba.dubbo.config.annotation.Reference;
import com.dtyunxi.rest.RestResponse;
import com.github.pagehelper.PageInfo;
import com.yunxi.baseDataToHub.pojo.sap.BankVO;
import com.yunxi.baseDataToHub.pojo.sap.PartnerInfoVO;
import com.yunxi.baseDataToHub.service.sap.SapDubboService;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.LoginInfo;
import com.yunxi.dealer.pojo.PartnerInvoiceVO;
import com.yunxi.dealer.pojo.PartnerVO;
import com.yunxi.dealer.service.PartnerInvoiceDubboService;
import com.yunxi.model.dealer.PartnerInvoice;
import com.yunxi.service.localService.dealer.PartnerInvoiceService;
import com.yunxi.util.S2SEnums;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 渠道商信息开票信息 服务实现类
 * </p>
 *
 * @author
 * @since 2019-05-14
 */
//@com.alibaba.dubbo.config.annotation.Service(version = "${demo.service.version}",
//        application = "${dubbo.application.id}",
//        protocol = "${dubbo.protocol.id}",
//        registry = "${dubbo.registry.id}"
//)
public class PartnerInvoiceDubboServiceImpl implements PartnerInvoiceDubboService {

   private static final Logger logger = LoggerFactory.getLogger(PartnerInvoiceDubboServiceImpl.class);


   @Autowired
   @Qualifier(value = "partnerInvoiceServiceImpl")
   PartnerInvoiceService partnerInvoiceService;

   @Autowired
   IChannelApi channelApi;

   @Autowired
   SapDubboService sapDubboServiceImpl;


   @Override
   public PartnerInvoiceVO getById(String id) throws InvocationTargetException, IllegalAccessException {
      PartnerInvoice partnerInvoice = partnerInvoiceService.getById(id);
      PartnerInvoiceVO partnerInvoiceVO = new PartnerInvoiceVO();
      BeanUtils.copyProperties(partnerInvoice, partnerInvoiceVO);
      return partnerInvoiceVO;
   }

   @Override
   public void saveOrUpdate(PartnerInvoiceVO partnerInvoiceVO) throws InvocationTargetException, IllegalAccessException {
      PartnerInvoice partnerInvoice = new PartnerInvoice();
      BeanUtils.copyProperties(partnerInvoiceVO, partnerInvoice);
      partnerInvoiceService.saveOrUpdate(partnerInvoice);
   }

   @Override
   @Transactional
   public void removeById(String id, String partnerCode,LoginInfo loginInfo) throws Exception {
      PartnerInvoice partnerInvoice = partnerInvoiceService.getById(id);
      partnerInvoice.setUpdateBy(loginInfo.getRealName());
      //将渠道发票信息传入中台
      RestResponse<Void> voidRestResponse = null;
      try {
         voidRestResponse = channelApi.delChannelInvoice(partnerInvoice.getInvoiceCode());
      } catch (Exception e) {
         TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
         logger.error(" ------- 中台调用出错 ___< 删除渠道发票信息  >--------"+e.getMessage());
         e.printStackTrace();
         return;
      }
      if (!voidRestResponse.getResultCode().equals("0")) {
         TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
         logger.error(" ------- 中台执行失败 ___< 删除渠道发票信息  >--------"+voidRestResponse.getResultMsg());
         return;
      }
      //将渠道发票信息传入SAP
      if (pms2SAP(partnerInvoice,partnerCode, S2SEnums.OperatorType.delete.getKey())) return;

      partnerInvoiceService.removeById(id);
   }

   @Override
   public PageInfo<PartnerInvoiceVO> findPageByVO(PartnerInvoiceVO partnerInvoiceVO, Integer pageNum, Integer pageSize) throws InvocationTargetException, IllegalAccessException {

      PageInfo<PartnerInvoiceVO> pageInfo = partnerInvoiceService.pageByVo(partnerInvoiceVO, pageNum, pageSize);

      return pageInfo;

   }

   @Override
   public PartnerInvoiceVO load(String accountCode) {
      return partnerInvoiceService.load(accountCode);
   }

   private boolean pms2SAP(PartnerInvoice partnerInvoice, String partnerCode , String op) throws Exception {
      PartnerInfoVO partnerInfoVO = setPropPartnerInfoVO(partnerInvoice ,partnerCode);
      partnerInfoVO.setIndicator(op);
      ApiResult apiResult1 = sapDubboServiceImpl.savePartnerInfoToSap(partnerInfoVO);
      if (apiResult1.getCode().equals(-1)) {
         TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
         logger.error("  ------- sapDubboServiceImpl.savePartnerInfoToSap 执行失败 ___删除渠道发票信息  ---------  ");
         return true;
      }
      return false;
   }

   private PartnerInfoVO setPropPartnerInfoVO(PartnerInvoice partnerInvoice ,String partnerCode) {
      List<BankVO> list = new ArrayList<>();

      BankVO bankVO = new BankVO();
      bankVO.setKunnr(partnerCode);
      bankVO.setBanks(partnerInvoice.getBankCode());
      bankVO.setBankl(partnerInvoice.getBankNo());
      bankVO.setBankn(partnerInvoice.getBankAccount());
      bankVO.setKoinh(partnerInvoice.getBankHolder());
      bankVO.setBanka(partnerInvoice.getBank());
      bankVO.setBankemail(partnerInvoice.getBankEmail());
      bankVO.setStreet(partnerInvoice.getBankAdd());
      list.add(bankVO);

      PartnerInfoVO partnerInfoVO = new PartnerInfoVO();
      partnerInfoVO.setBank(list);
      return partnerInfoVO;
   }
}
